home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / editors / mutt / me2s_pl7.zoo / mu_edit2 / util / fname.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-05  |  2.6 KB  |  111 lines

  1. static char rcsid[] = "$Id: fname.c,v 1.1 1992/09/06 19:31:32 mike Exp $";
  2.  
  3. /* $Log: fname.c,v $
  4.  * Revision 1.1  1992/09/06  19:31:32  mike
  5.  * Initial revision
  6.  *
  7.  */
  8.  
  9. /*
  10.  *  fname.c:  Mess with file names, paths.
  11.  *  C Durland    Public Domain
  12.  *  See also:  fpath.c
  13.  */
  14.  
  15. #include <stdio.h>
  16. #include "os.h"
  17.  
  18. #define DOT   '.'
  19. #define DRIVE ':'        /* for MS-DOS anyway */
  20.  
  21. char *strcpy();
  22.  
  23.     /* Point to the end of the path.
  24.      * The resulting path is such that you can cd to it.
  25.      * Use nanex() if you want to append a new name.
  26.      * To leave just the path:  *pathend(fname) = '\0';
  27.      * Examples: (^ shows what pathend() points to)
  28.      *   "foo/bar", "/foo", "/", "/foo/"
  29.      *       ^        ^       ^       ^
  30.      *   "..", ".", "/..", "foo."
  31.      *      ^    ^      ^   ^
  32.      *   ".foo", "foo", ""
  33.      *    ^       ^      ^
  34.      * MS-DOS:
  35.      *   "A:", "A:foo", "A:/", "A:/foo"
  36.      *      ^     ^         ^      ^
  37.      */
  38. char *pathend(fname) register char *fname;
  39. {
  40.   register char *ptr = fname + strlen(fname);
  41.  
  42.   while (ptr != fname)
  43.   {
  44.     ptr--;
  45.     if (ISSLASH(*ptr))
  46.     {
  47.       if (ptr == fname) ptr++;            /* /, /foo */
  48. #if MSDOZ || ATARI
  49.       else if (ptr[-1] == DRIVE) ptr++;        /* A:/, A:/foo */
  50. #endif
  51.       break;
  52.     }
  53. #if MSDOZ || ATARI
  54.     if (*ptr == DRIVE) { ptr++; break; }    /* A:, A:foo */
  55. #endif
  56.   }
  57.   if (*ptr == DOT)            /* might have . or .. */
  58.   {
  59.     if (ptr[1] == '\0') ptr++;                /* "." */
  60.     else
  61.       if (ptr[1] == DOT && ptr[2] == '\0') ptr += 2;    /* ".." */
  62.   }    
  63.   return ptr;
  64. }
  65.  
  66.     /* Point to the filename & extension.
  67.      * Assumes a real filename & no trailing junk.
  68.      */
  69. char *nanex(fname) register char *fname;
  70. {
  71.   fname = pathend(fname);
  72.   if (ISSLASH(*fname)) fname++;
  73.   return fname;
  74. }
  75.  
  76.     /* Point to start of extension (ie the '.')
  77.      *   The first "." after the last "/".
  78.      * Assumes a real filename.
  79.      * Notes:
  80.      *   foo.c => ".c"
  81.      *   foo => ""
  82.      *   .foo.bar => ".bar" (UNIX hidden files).
  83.      *   .foo => ".foo"  (csh does this).
  84.      *   y.tab.c => ".c".
  85.      *   "" => ""
  86.      *   foo. => "."
  87.      */
  88. char *ext(fname) register char *fname;
  89. {
  90.   char *ptr, *end;
  91.  
  92.   fname = nanex(fname);                /* start of the file name */
  93.   end = ptr = fname + strlen(fname);        /* end of file name ('\0') */
  94.  
  95.   for ( ; (ptr != fname) && (*ptr != DOT); ptr--) ;
  96.  
  97.   if (*ptr != DOT) return end;            /* no extension */
  98.   return ptr;
  99. }
  100.  
  101.     /* for UNIX: .foo => .foo.ext?  (not yet)
  102.      * ..\fred.foo
  103.      *  ext MUST start with a '.'
  104.      */
  105. char *new_ext(newname,nameext,newext) char *newname, *nameext, *newext;
  106. {
  107.   strcpy(ext(strcpy(newname,nameext)),newext);
  108.   return newname;
  109. }
  110.  
  111.